using UnityEngine;
using System.Collections;
public class DungeonBSPNode : MonoBehaviour 
{
//	public enum Orientation { Horizontal, Vertical};
		
//	public Orientation SplitOrientation;
	
//	public EdgeRect RegionEdges;

//	public float SplitLocation;
	
//	public DungeonBSPNode LeftChild {get; set;}
//	public DungeonBSPNode RightChild {get; set;}
	
//	static public Random rand;
	
//	static public float SmallestPartitionSize = .2f;
	
//	static public float MaxPartitionSizeRatio = 1.5f;
	
//	static public float HomogeneityFactor = 0.25f;
		
//	public bool RoomBuilt = false;
	
//	public string DebugName = "unset";
//	static public int DebugValue = 0;
	
	
//	public void dungeonBSPNode(float startX, float startY, float endX, float endY)
//	{
//		RegionEdges.edgeRect(startX, startY, endX, endY);
//	}
//	public void Partition()
//	{
//		if (RegionEdges.Width / RegionEdges.Height > MaxPartitionSizeRatio)
//			SplitOrientation = Orientation.Horizontal;
//		else if (RegionEdges.Height / RegionEdges.Width > MaxPartitionSizeRatio)
//			SplitOrientation = Orientation.Vertical;
//		else
//			SplitOrientation = (rand.Next(2) == 1) ? Orientation.Horizontal : Orientation.Vertical;
		
//		if (SplitOrientation == Orientation.Horizontal)
//		{
//			SplitLocation = RegionEdges.Left + HomogenizedRandomValue() * RegionEdges.Width;
//			LeftChild = new dungeonBSPNode(RegionEdges.Left, RegionEdges.Top, SplitLocation, RegionEdges.Bottom);
//			RightChild = new dungeonBSPNode(SplitLocation, RegionEdges.Top, RegionEdges.Right, RegionEdges.Bottom);
			
//			SetDebugNames();
			
//			if (WeShouldSplit(SplitLocation - RegionEdges.Left))
//				LeftChild.Partition();
//			if (WeShouldSplit(RegionEdges.Right - SplitLocation))
//				RightChild.Partition();
//           }
//		else
//		{
//			SplitLocation = RegionEdges.Top + HomogenizedRandomValue() * RegionEdges.Height;
//			LeftChild = new dungeonBSPNode(RegionEdges.Left, RegionEdges.Top, RegionEdges.Right, SplitLocation);
//			RightChild = new dungeonBSPNode(RegionEdges.Left, SplitLocation, RegionEdges.Right, RegionEdges.Bottom);
			
//			SetDebugNames();
			
//			if (WeShouldSplit(SplitLocation - RegionEdges.Top))
//				LeftChild.Partition();
//			if (WeShouldSplit(RegionEdges.Bottom - SplitLocation))
//				RightChild.Partition();
//		}
//	}
	
//	private void SetDebugNames()
//	{
//		DebugValue++;
//		LeftChild.DebugName = DebugValue + "L";
//		RightChild.DebugName = DebugValue + "R";
//	}
	
/*	public void PartitionAround(Rectangle boundingRect)
	{
		float startX = (float)boundingRect.Left / Map.MapWidth;
        float startY = (float)boundingRect.Top / Map.MapHeight;
        float endX = (float)boundingRect.Right/ Map.MapWidth;
        float endY = (float)boundingRect.Bottom/ Map.MapHeight;

		
        this.SplitOrientation = Orientation.Vertical;
        this.SplitLocation = startX;
        if (startY == RegionEdges.Top)
                LeftChild = null;    
        else
        {
			LeftChild = new dungeonBSPNode(RegionEdges.Left, RegionEdges.Top, RegionEdges.Right, startY);
			if (WeShouldSplit(startY - RegionEdges.Top))
				LeftChild.Partition();
        }
        RightChild = new dungeonBSPNode(RegionEdges.Left, startY, RegionEdges.Right, RegionEdges.Bottom);

        RightChild.SplitOrientation = Orientation.Horizontal;
        RightChild.SplitLocation = startY;
        if (startX == RegionEdges.Left)
                RightChild.LeftChild = null;    
        else
        {
			RightChild.LeftChild = new dungeonBSPNode(RegionEdges.Left, startY, startX, RegionEdges.Bottom);
			if (WeShouldSplit(startX - RegionEdges.Left))
				RightChild.LeftChild.Partition();
        }
        RightChild.RightChild = new dungeonBSPNode(startX, startY, RegionEdges.Right, RegionEdges.Bottom);

        RightChild.RightChild.SplitOrientation = Orientation.Vertical;
        RightChild.RightChild.SplitLocation = endY;
        if (RegionEdges.Bottom == endY)
                RightChild.RightChild.RightChild = null;
        else
        {
			RightChild.RightChild.RightChild = new dungeonBSPNode(startX, endY, RegionEdges.Right, RegionEdges.Bottom);
			if (WeShouldSplit(RegionEdges.Bottom - endY))
				RightChild.RightChild.RightChild.Partition();
        }
        RightChild.RightChild.LeftChild = new dungeonBSPNode(startX, startY, RegionEdges.Right, endY);

        RightChild.RightChild.LeftChild.SplitOrientation = Orientation.Horizontal;
        RightChild.RightChild.LeftChild.SplitLocation = endX;
        if (RegionEdges.Right == endX)
			RightChild.RightChild.LeftChild.RightChild = null;
        else
        {
			RightChild.RightChild.LeftChild.RightChild = new dungeonBSPNode(endX, startY, RegionEdges.Right, endY);
            if (WeShouldSplit(RegionEdges.Right - endX))
				RightChild.RightChild.LeftChild.RightChild.Partition();
        }

        RightChild.RightChild.LeftChild.LeftChild = new dungeonBSPNode(startX, startY, endX, endY);

        RightChild.RightChild.LeftChild.LeftChild.RoomBuilt = true;
        RightChild.RightChild.LeftChild.LeftChild.BoundingRect = boundingRect;
	}*/
	
//	private bool WeShouldSplit(float partitionSize)
//	{
//		if(partitionSize > SmallestPartitionSize && partitionSize < SmallestPartitionSize * 2.0 && rand.NextDouble() <= .1)
//			return false;
		
//		return partitionSize > SmallestPartitionSize;
//	}
	
//	private float HomogenizedRandomValue()
//	{
//		return (float)(0.5 - (rand.NextDouble() * HomogeneityFactor));
//	}
	
/*	public IList<dungeonBSPNode> GetRoomRegions()
	{
		IList<dungeonBSPNode> list = new IList<dungeonBSPNode>();
		EnumerateLeafNodes(list);
		return list;
	}*/
	
/*	public void EnumerateLeafNodes(IList<dungeonBSPNode> list)
	{
		if(LeftChild != null || RightChild != null)
		{
			if(LeftChild != null)
				LeftChild.EnumerateLeafNodes(list);
			if(RightChild != null)
				RightChild.EnumerateLeafNodes(list);
		}
		else if (RoomBuilt)
			list.Add(this);
	}*/
	
/*	public void BottomsUpByLevelEnumerate(RoomGeneratorDelegate roomGenerator, CorridorGeneratorDelegate corridorGenerator)
	{
		Stack stack1 = new Stack();
        Stack stack2 = new Stack();
        stack1.Push(this);
        while (stack1.Count > 0)
		{
			dungeonBSPNode currentNode = (dungeonBSPNode)stack1.Pop();
            stack2.Push(currentNode);
            if (currentNode.LeftChild != null)
				stack1.Push(currentNode.LeftChild);
            if (currentNode.RightChild != null)
                stack1.Push(currentNode.RightChild);
		}

        while (stack2.Count > 0)
        {
            dungeonBSPNode currentNode = (dungeonBSPNode)stack2.Pop();
            if (currentNode.LeftChild == null && currentNode.RightChild == null)
            {
				// Leaf node - create a room
				if (!currentNode.RoomBuilt && roomGenerator != null)
					roomGenerator(currentNode);
            }
            else
            {
				// non-leaf node; create corridor
				if (corridorGenerator != null && (currentNode.LeftChild.RoomBuilt || currentNode.RightChild.RoomBuilt))
					corridorGenerator(currentNode);
            }
		}
	}*/
	
//	public void OnGUI()
//	{
		//float w = Map.MapWidth * 8;
		//float h = Map.MapHeight * 8;
//		Graphics.DrawTexture(new Rect(5,0,5,512), texture);
		//gr.FillRectangle(Brushes.Black, (float)RegionEdges.Left * w + 10, (float)RegionEdges.Top * h + 10, (float)(RegionEdges.Width) * w, (float)(RegionEdges.Height) * h);
		//gr.DrawRectangle(Pens.Red, (float)RegionEdges.Left * w + 10, (float)RegionEdges.Top * h + 10, (float)(RegionEdges.Width) * w, (float)(RegionEdges.Height) * h);
		
//		if(LeftChild != null)
//			LeftChild.Render(gr);
//		if(RightChild != null)
//			RightChild.Render(gr);
//	}
	
	// Use this for initialization
	void Start () 
	{
//		RegionEdges = FindObjectOfType(typeof(EdgeRect))as EdgeRect;
	}
	
	// Update is called once per frame
	void Update () 
	{}
}
